package cc.linker.steplink.crm.merchanth5.service.form;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.FormLoginDto;
import cc.linker.steplink.crm.merchanth5.dao.FormH5Dao;
import cc.linker.steplink.crm.merchanth5.pojo.form.*;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: CKQ
 * @DateTime: 2020/8/11 9:13
 * @Description: TODO
 */
@Slf4j
@Service
public class FormService {
    @Value("${system.scrm.formAppId}")
    private String formAppId;
    @Value("${system.scrm.formUrl}")
    private String formUrl;
    @Autowired
    private HttpService httpService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private FormH5Dao formH5Dao;

    /**
    * @Description：通过应用ID获取题目和选项列表
    * @DateTime：2020/8/11 15:14
    * @Params：
    * @Return：
    */
    public List<ExFormAttrPo> getFormAttrListByAppId(String formId) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appId", formAppId);
        headerMap.put("Content-Type", "application/json");
        try {
            HashMap params = new HashMap();
            if(!StringUtils.isEmpty(formId)){
                params.put("formId",formId);
            }else{
                params = null;
            }
            String result = httpService.doGet(formUrl + "/form/external/getFormAttrListByAppId", params, headerMap);
            if (StringUtils.isEmpty(result)) {
                throw GlobalException.build("获取题目数据失败");
            }
            Map resultMap = (Map) JSON.parse(result);
            if (resultMap != null) {
                if (resultMap.get("code").toString().equals("200") && resultMap.get("data") != null) {
                    return JSON.parseArray(resultMap.get("data").toString(), ExFormAttrPo.class);
                }
            }
        } catch (Exception ex) {
            throw GlobalException.build("授权登录失败");
        }
        return null;
    }



    public void insertCustomerLabel(ApplyFormDto applyFormDto){
        if(applyFormDto!=null){
//            ApplyFormDto applyFormDto = JSON.parseObject(msg, ApplyFormDto.class);
            if(applyFormDto!=null && applyFormDto.getApplyFormAttrItemDtos()!=null){
                List<ApplyFormAttrItemDto> applyFormAttrItemDtos = applyFormDto.getApplyFormAttrItemDtos();
                if(applyFormAttrItemDtos.size()>0){
                    for(ApplyFormAttrItemDto applyFormAttrItemDto : applyFormAttrItemDtos){
                        String attrType = applyFormAttrItemDto.getAttrType();
                        if("checkbox".equals(attrType) || "radio".equals(attrType) || "select".equals(attrType)){
                            List<Long>  checkItemIds = applyFormAttrItemDto.getCheckItemIds();
                            if(checkItemIds!=null && checkItemIds.size()>0){
                                StringBuffer sb = new StringBuffer();
                                for(Long checkItemId : checkItemIds){
                                    sb. append(checkItemId+",");
                                }
                                String newStr = sb.toString();
                                String str3 =newStr.substring(0, newStr.length() - 1);
                                String labelIdsString = formH5Dao.getLabelIds(str3);
                                if(!StringUtils.isEmpty(labelIdsString)){
                                    String str[] = labelIdsString.split(",");
                                    List labelIds = Arrays.asList(str);
                                    if(labelIds!=null && labelIds.size()>0){
                                        formH5Dao.insertCustomerLabel(labelIds,applyFormDto.getCustomerId());
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }

    }


    public static void main(String[] args) {
        ApplyFormDto applyFormDto = new ApplyFormDto();
        applyFormDto.setCustomerId("");
    }

}
